﻿#ifndef LIBXLFUNCTION_H
#define LIBXLFUNCTION_H

#include <QString>

#include <libxl.h>

namespace libxl
{
#ifdef _UNICODE
    inline wchar_t *QStringToTCHAR(const QString &s)
    {
        int size = s.size() + 1;
        wchar_t *c = new wchar_t[size];
        memset(c, 0, size * sizeof(wchar_t));
        s.toWCharArray(c);
        return c;
    }

    inline QString TCHARToQString(const wchar_t *c, int length = -1)
    {
        return c == nullptr ? QString() : QString::fromWCharArray(c, length);
    }

#else
    inline char *QStringToTCHAR(const QString &s)
    {
        QByteArray ba = s.toUtf8();
        int size = ba.size() + 1;
        char *c = new char[size];
        memset(c, 0, size * sizeof(char));
        strcpy_s(c, size, ba.constData());
        return c;
    }

    inline QString TCHARToQString(const char *c, int length = -1)
    {
        return c == nullptr ? QString() : QString::fromUtf8(c, length);
    }

#endif

    inline void activate(Book *book)
    {
        static const QString sc_strName = QStringLiteral("Halil Kural");
        static const QString sc_strKey = QStringLiteral("windows-2723210a07c4e90162b26966a8jcdboe");

        auto name = QStringToTCHAR(sc_strName);
        auto key = QStringToTCHAR(sc_strKey);
        book->setKey(name, key);
        delete []name;
        delete []key;

#ifdef _UNICODE
        book->setLocale("UTF-16");
#else
        book->setLocale("UTF-8");
#endif
    }
}

#endif // LIBXLFUNCTION_H
